package com.espirit.eap.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ImageUtils {
	
	private static final Log logger = LogFactory.getLog(ImageUtils.class);
	
	/**
	 * 对图片进行放大
	 * 
	 * @param originalImage
	 *            原始图片
	 * @param times
	 *            放大倍数
	 * @return
	 */
	public static BufferedImage zoomInImage(BufferedImage originalImage, Integer times) {
		int width = originalImage.getWidth() * times;
		int height = originalImage.getHeight() * times;
		BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();
		return newImage;
	}
	
	public static BufferedImage zoomInImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
		// the original size is too small, don't zoom in
		if (targetWidth > originalImage.getWidth() && targetHeight > originalImage.getHeight()) {
			return originalImage;
		}
		
		// calculate the scale
		float scaleWidth = (float)targetWidth / originalImage.getWidth();
		float scaleHeight = (float)targetHeight / originalImage.getHeight();
		float scale;
		if (scaleWidth < scaleHeight) {
			scale = scaleWidth;
			targetHeight = (int) (originalImage.getHeight() * scale);
		} else {
			scale = scaleHeight;
			targetWidth = (int) (originalImage.getWidth() * scale);
		}
		
		// zoom in
		BufferedImage newImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
		g.dispose();
		return newImage;
	}

	/**
	 * 对图片进行放大
	 * 
	 * @param srcPath
	 *            原始图片路径(绝对路径)
	 * @param newPath
	 *            放大后图片路径（绝对路径）
	 * @param times
	 *            放大倍数
	 * @return 是否放大成功
	 */
	public static boolean zoomInImage(String srcPath, String newPath, Integer times) {
		BufferedImage bufferedImage = null;
		try {
			File of = new File(srcPath);
			if (of.canRead()) {
				bufferedImage = ImageIO.read(of);
			}
		} catch (IOException e) {
			logger.error("read image", e);
			return false;
		}
		if (bufferedImage != null) {
			bufferedImage = zoomInImage(bufferedImage, times);
			try {
				ImageIO.write(bufferedImage, "JPG", new File(newPath)); // 保存修改后的图像,全部保存为JPG格式
			} catch (IOException e) {
				logger.error("write image", e);
				return false;
			}
		}
		return true;
	}
	
	public static boolean zoomInImage(File input, String newPath, int targetWidth, int targetHeight) {
		BufferedImage bufferedImage = null;
		try {
			if (input.canRead()) {
				bufferedImage = ImageIO.read(input);
			}
		} catch (IOException e) {
			logger.error("read image", e);
			return false;
		}
		if (bufferedImage != null) {
			bufferedImage = zoomInImage(bufferedImage, targetWidth, targetHeight);
			
			try {
				File newPathFile = new File(newPath);
				newPathFile.getParentFile().mkdirs();
				ImageIO.write(bufferedImage, "JPG", new File(newPath)); // 保存修改后的图像,全部保存为JPG格式
			} catch (IOException e) {
				logger.error("write image", e);
				return false;
			}
		}
		return true;
	}

	/**
	 * 对图片进行缩小
	 * 
	 * @param originalImage
	 *            原始图片
	 * @param times
	 *            缩小倍数
	 * @return 缩小后的Image
	 */
	public static BufferedImage zoomOutImage(BufferedImage originalImage, Integer times) {
		int width = originalImage.getWidth() / times;
		int height = originalImage.getHeight() / times;
		BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
		Graphics g = newImage.getGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();
		return newImage;
	}

	/**
	 * 对图片进行缩小
	 * 
	 * @param srcPath
	 *            源图片路径（绝对路径）
	 * @param newPath
	 *            新图片路径（绝对路径）
	 * @param times
	 *            缩小倍数
	 * @return 保存是否成功
	 */
	public static boolean zoomOutImage(String srcPath, String newPath, Integer times) {
		BufferedImage bufferedImage = null;
		try {
			File of = new File(srcPath);
			if (of.canRead()) {
				bufferedImage = ImageIO.read(of);
			}
		} catch (IOException e) {
			logger.error("read image", e);
			return false;
		}
		if (bufferedImage != null) {
			bufferedImage = zoomOutImage(bufferedImage, times);
			try {
				ImageIO.write(bufferedImage, "JPG", new File(newPath)); // 保存修改后的图像,全部保存为JPG格式
			} catch (IOException e) {
				logger.error("write image", e);
				return false;
			}
		}
		return true;
	}
}
